package cn.ingenic.glasssync.camera;

import android.content.Context;
import android.util.Log;
import android.os.Handler;
import android.provider.Settings;
import cn.ingenic.glasssync.services.SyncData;
import cn.ingenic.glasssync.services.SyncModule;
import cn.ingenic.glasssync.services.SyncException;

public class TakePictureModule extends SyncModule {
	private final String TAG = "TakePictureModule";
	private final static String MODULE_NAME = "picture_module";
	public static final int MSG_RECEIVE_PICTURE_DATA_1 = 1001;
	public static final int MSG_RECEIVE_PICTURE_DATA_2 = 1002;
	private final int TYPE_SEND_PICTURE_DATA_1 = 0x11;
	private final int TYPE_SEND_PICTURE_DATA_2 = 0x12;
        private final int TYPE_SEND_SERIAL = 0x13;

        private final int TYPE_SEND_PICTURE_CIRCLE_DATA_1 = 0x14;
        private final int TYPE_SEND_PICTURE_CIRCLE_DATA_2 = 0x15;
        private final int TYPE_SEND_PICTURE_STOP = 0x20;

	private final String KEY_TYPE = "type";
	private final String KEY_DATA = "pic_data";
        private final String KEY_SERIAL = "pic_serial";
	public static TakePictureModule sInstance;
	private Handler mHandler;

        private static int Circulation_camera = 0;

    static int send_cmd_cnt = 0;
    static int onRec_cnt = 0;

	private TakePictureModule(Context context) {
		super(MODULE_NAME, context);
		Log.e(TAG,"TakePictureModule --- init");
	}

	public static TakePictureModule getInstance(Context c) {
		if (null == sInstance)
			sInstance = new TakePictureModule(c);
		return sInstance;
	}

	@Override
	protected void onCreate() {
	    Log.e(TAG,"TakePictureModule --- onCreate");
	}

        public void send_cmd(int cmd){
	    SyncData data = new SyncData();

	    switch(cmd){
	    case TYPE_SEND_PICTURE_CIRCLE_DATA_1:
		Log.e(TAG, "send circle 1 cmd " + send_cmd_cnt++);
		data.putInt(KEY_TYPE, TYPE_SEND_PICTURE_CIRCLE_DATA_1);
		break;

	    case TYPE_SEND_PICTURE_CIRCLE_DATA_2:
		Log.e(TAG, "send circle 2 cmd " + send_cmd_cnt++);
		data.putInt(KEY_TYPE, TYPE_SEND_PICTURE_CIRCLE_DATA_2);
		break;

	    case TYPE_SEND_PICTURE_STOP:
		Log.e(TAG, "send stop cmd");
		Circulation_camera = 0;
		send_cmd_cnt = 0;
		onRec_cnt = 0;
		data.putInt(KEY_TYPE, TYPE_SEND_PICTURE_STOP);
		break;
	    }

	    try {
		send(data);
	    } catch (SyncException e) {
		Log.e(TAG, "send_cmd : " + e);
	    }	
	}

	@Override
	protected void onRetrive(SyncData data) {
		super.onRetrive(data);
		Log.d(TAG,"onRetrive in");
		int type = data.getInt(KEY_TYPE);
		byte[] picData1;
		byte[] picData2;
		switch (type) {
		case TYPE_SEND_PICTURE_DATA_1:
			picData1 = data.getByteArray(KEY_DATA);
			Log.e(TAG, "onRetrive rcv data1 " + onRec_cnt++);
			Log.d(TAG,"picData = " + picData1.length + "--mHandler = " + mHandler);
			//Circulation_camera = 1;
			if (null != mHandler) {
				mHandler.obtainMessage(MSG_RECEIVE_PICTURE_DATA_1, picData1).sendToTarget();
			}
			if (Circulation_camera == 1){
			    Log.e(TAG, "send circle 1 cmd");
			    send_cmd(TYPE_SEND_PICTURE_CIRCLE_DATA_1);
			}
			break;

		case TYPE_SEND_PICTURE_DATA_2:
			picData2 = data.getByteArray(KEY_DATA);
			Log.e(TAG, "onRetrive rcv data2 " + onRec_cnt++);
			Log.d(TAG,"picData = " + picData2.length + "--mHandler = " + mHandler);
			//Circulation_camera = 1;
			if (null != mHandler) {
				mHandler.obtainMessage(MSG_RECEIVE_PICTURE_DATA_2, picData2).sendToTarget();
			}
			if (Circulation_camera == 1){
			    Log.e(TAG, "send circle 2 cmd");
			    send_cmd(TYPE_SEND_PICTURE_CIRCLE_DATA_2);
			}
			break;

		case TYPE_SEND_PICTURE_CIRCLE_DATA_1:
			picData1 = data.getByteArray(KEY_DATA);
			if (Circulation_camera == 0)
			    break;
			Log.e(TAG, "onRetrive rcv cricle data1 " + onRec_cnt++);
			Log.d(TAG,"picData = " + picData1.length + "--mHandler = " + mHandler);
			if (null != mHandler) {
				mHandler.obtainMessage(MSG_RECEIVE_PICTURE_DATA_1, picData1).sendToTarget();
			}
			if (Circulation_camera == 1)
			    send_cmd(TYPE_SEND_PICTURE_CIRCLE_DATA_1);
			break;

		case TYPE_SEND_PICTURE_CIRCLE_DATA_2:
			picData2 = data.getByteArray(KEY_DATA);
			if (Circulation_camera == 0)
			    break;
			Log.e(TAG, "onRetrive rcv cricle data2 " + onRec_cnt++);
			Log.d(TAG,"picData = " + picData2.length + "--mHandler = " + mHandler);
			if (null != mHandler) {
				mHandler.obtainMessage(MSG_RECEIVE_PICTURE_DATA_2, picData2).sendToTarget();
			}
			if (Circulation_camera == 1)
			    send_cmd(TYPE_SEND_PICTURE_CIRCLE_DATA_2);
			break;

		case TYPE_SEND_SERIAL:
		    int serialnum = data.getInt(KEY_SERIAL);
		    Log.e(TAG, "serialnum " + serialnum);
		    break;
		}
	}

	public void registerHandler(Handler handler) {
		mHandler = handler;
	}

	public void unRegisterHandler() {
		mHandler = null;
	}
}
